package com.wteam.dragon.model.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wteam.dragon.model.pojo.bean.Article;
import com.wteam.dragon.model.pojo.vo.ArticleVo;
import com.wteam.dragon.model.service.ArticleService;
import com.wteam.dragon.system.aop.log.log.Log;
import com.wteam.dragon.system.apps.security.annotation.AnonymousAccess;
import com.wteam.dragon.system.basebean.bean.ResultMessage;
import com.wteam.dragon.system.basebean.vo.PageVo;
import com.wteam.dragon.system.util.BeanUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author 陈晓辉
 * @Date 2020/5/1 22:52
 */
@RestController
@Api(tags = "源：文章")
@RequestMapping("article")
@RequiredArgsConstructor
public class  ArticleController {
    private final ArticleService articleService;

    @Log("添加文章")
    @ApiOperation("添加文章")
    @PostMapping("addArticle")
    @PreAuthorize("@el.check('addArticle')")
    public ResultMessage addArticle(@Validated ArticleVo articleVo){
        Article article = BeanUtil.from(articleVo, Article.class);
        return articleService.addArticle(article);
    }

    @Log("删除文章")
    @ApiOperation("删除文章")
    @DeleteMapping("delArticle")
    @PreAuthorize("@el.check('delArticle')")
    public ResultMessage delArticle(@RequestParam() List<Long> ids){
        return articleService.delArticle(ids);
    }

    @Log("更改文章")
    @ApiOperation("更改文章")
    @PutMapping("editArticle")
    @PreAuthorize("@el.check('editArticle')")
    public ResultMessage editArticle(ArticleVo articleVo){
        Article article = BeanUtil.from(articleVo, Article.class);
        return articleService.editArticle(article);
    }

    @Log("获得文章列表")
    @ApiOperation("获得文章列表")
    @GetMapping("pageArticle")
    @AnonymousAccess
    public ResultMessage pageArticle(PageVo<Article> pageVo,
                                     String title, Long typeId, Boolean isShow){
        Page<Article> page = BeanUtil.fromPage(pageVo);
        return articleService.pageArticle(page, title, typeId, isShow);
    }

    @Log("获得文章详情")
    @ApiOperation("获得文章详情")
    @GetMapping("getArticle")
    @AnonymousAccess
    public ResultMessage getArticle(Long id){
        return articleService.getArticle(id);
    }
}
